package com.group.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.group.pojo.TbCustomer;

public class LoginInterceptor implements HandlerInterceptor{

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
		//判断用户是否登录，如果没有登录，重定向到登录页面，不放行
		//如果登录了，就放行
		Object customers = request.getSession().getAttribute("CUSTOMER_SESSION");
		if (null != customers) {
			TbCustomer customer = (TbCustomer) customers;
			if (null != customer.getId()) {
				System.out.println("LoginInterceptor拦截器："+customer.getCustomerName()+"已登录。");
				return true;
			}
		}
		//http://localhost:8080/contract-customer/bill/page.action
		StringBuffer url = request.getRequestURL();
		response.sendRedirect(request.getContextPath() + "/login.jsp?redirect="+url);
		System.out.println("LoginInterceptor拦截器：未登录，执行方法前拦截成功！");
		return false;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		//System.out.println("LoginInterceptor拦截器：执行方法后");
	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		//System.out.println("LoginInterceptor拦截器：页面渲染后");
	}

}
